<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Works extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'works';
    protected $works_txt = 'works_txt';
    protected $works_tag = 'works_tags';


    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $work_title = $request->input('title');
        $work_type = $request->input('type');
        $tag_id = $request->input('tag_id');

        $query = DB::connection($this->connection)->table($this->works_txt);

        // todo 搜索
        if ($work_title)$query->where($this->works_txt.'.title',$work_title);
        if ($work_type)$query->where($this->works_txt.'.type',$work_type);
        if ($tag_id)$query->where($this->works_txt.'.tag_id',$tag_id);

        $list = $query->where($this->works_txt.'.status',1)
            ->leftJoin('works_tags',$this->works_txt.'.tag_id','works_tags.tag_id')
            ->orderBy('id','desc')
            ->paginate($limit,['*'],'page',$page);
        foreach ($list as $row){
            $row->txt = substr($row->txt ,1,(strlen($row->txt) - 2));
        }
       return $list;
    }
    public function getTag(){
        return DB::connection($this->connection)->table($this->works_tag)
            ->get(['tag_id','status','tag']);
    }
    public function addOrupdate($request){
        $id = $request->input('id');
        $title = $request->input('title');
        $txt = $request->input('txt');
        $type = $request->input('type');
//        $status = $request->input('status');
        $tag_id = $request->input('tag_id');
        $data = [
            'title'     => $title,
            'txt'       => $txt,
            'type'      => $type,
            'status'    => 1,
            'tag_id'    => $tag_id
        ];
//        var_dump($data);
        $url = getKkaudioApiUrl();
        if ($id){
            $data['id'] = $id;
            return curl_request($url.'/inner/works/txt/update',$data, 'urlencoded', 'POST');
        }
        return curl_request($url.'/inner/works/txt/create',$data, 'urlencoded', 'POST');
    }
    public function addOrupdateTag($request){
        $tag_id = $request->input('tag_id');
        $status = $request->input('status');
        $tag = $request->input('tag');

        $url = getKkaudioApiUrl();
        $data = [
            'status' => $status,
            'tag'   => $tag,
        ];
        if ($tag_id){
            $data['tag_id'] = $tag_id;
            return curl_request($url.'/inner/works/update',$data, 'urlencoded', 'POST');
        }
        return curl_request($url.'/inner/works/create',$data, 'urlencoded', 'POST');
    }
    public function delTag($tag_id){
        $url = getKkaudioApiUrl();
        $data = [
            'id' => $tag_id,
        ];
        return curl_request($url.'/inner/works/delete',$data, 'urlencoded', 'POST');
    }
    public function delTxt($id){
        $url = getKkaudioApiUrl();
        $data = [
            'id' => $id,
        ];
        return curl_request($url.'/inner/works/del',$data, 'urlencoded', 'POST');
    }
}
